package servlet;

import dao.*;
import dao.daoimpl.*;
import bean.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//登录页面服务器
@WebServlet(name="LoginServlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        String account = request.getParameter("userAccount");
        String psw = request.getParameter("userPassword");
        UserDao userDao=new UserDaoImpl();/*用户操作接口的实现类*/
        ArticleDao articleDao=new ArticleDaoImpl();/*文章接口的实现类*/
        LabelDao labelDao=new LabelDaoImpl();/*标签接口的实现类*/
        CommentDao commentDao=new CommentDaoImpl();/*评论接口的实现类*/
        CategoryDao categoryDao=new CategoryDaoImpl();/*目录接口的实现类*/
        CollectionDao collectionDao=new CollectionDaoImpl();/*收藏接口的实现类*/
        LikeDao likeDao=new LikeDaoImpl();/*点赞接口的实现类*/
        AdministratorDao administratorDao=new AdministratorImpl();
            User user = null;
            try {
                user = userDao.login(account, psw);/*验证密码与账号是否正确*/
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
            if (user != null) {
                /*1用来标识登陆者为管理员，0用来标识为登陆者为普通用户*/
                if(user.getIsAdmin()==1) {
                    System.out.println("管理员登录成功");
                    try {
                        request.getSession().setAttribute("administratorArticles",administratorDao.getAllArticles());/*所有博客文章*/
                        request.getSession().setAttribute("administratorUsers",administratorDao.getAllUsers());/*所有用户*/
                        request.getSession().setAttribute("administratorCommands",administratorDao.getAllCommand());/*所有推荐博客*/
//                        request.getSession().setAttribute("administratorCommands",);/*广告图合集*/
                        request.getSession().setAttribute("userArticles",articleDao.getArticles(user));/*用户博客信息*/
                        request.getSession().setAttribute("userCollections", articleDao.getCollectedArticles(user));/*用户收藏的博客*/
                        request.getSession().setAttribute("userCollected",collectionDao.getCollections(user));/*用户收藏*/
                        request.getSession().setAttribute("userLikes",articleDao.getLikedArticles(user));/*用户点赞的博客*/
                        request.getSession().setAttribute("userLiked",likeDao.getLikes(user));/*用户点赞*/
                        request.getSession().setAttribute("userCategories",categoryDao.getCategories(user));/*用户目录*/
                        request.getSession().setAttribute("userLabels",labelDao.getUserLabels(user));/*用户标签*/
                        request.getSession().setAttribute("userPaginate",articleDao.getPaginateArticle(0,3,user));/*分页信息*/
                        request.getSession().setAttribute("allPage",articleDao.getPage(user));/*分页数量*/
                        request.getSession().setAttribute("page",0);/*当前页面*/
                        request.getSession().setAttribute("cnt",0);/*数量控制*/
                        request.getSession().setAttribute("user", user);/*用户信息*/
                        response.sendRedirect(request.getContextPath() + "/administratorPage.jsp");
                    } catch (SQLException sqlException) {
                        sqlException.printStackTrace();
                    }
                }else {
                    System.out.println("用户登录成功");
                    try {
                        request.getSession().setAttribute("userArticles",articleDao.getArticles(user));/*用户博客信息*/
                        request.getSession().setAttribute("userCollections", articleDao.getCollectedArticles(user));/*用户收藏的博客*/
                        request.getSession().setAttribute("userCollected",collectionDao.getCollections(user));/*用户收藏*/
                        request.getSession().setAttribute("userLikes",articleDao.getLikedArticles(user));/*用户点赞的博客*/
                        request.getSession().setAttribute("userLiked",likeDao.getLikes(user));/*用户点赞*/
                        request.getSession().setAttribute("userCategories",categoryDao.getCategories(user));/*用户目录*/
                        request.getSession().setAttribute("userLabels",labelDao.getUserLabels(user));/*用户标签*/
                        request.getSession().setAttribute("userPaginate",articleDao.getPaginateArticle(0,3,user));/*分页信息*/
                        request.getSession().setAttribute("allPage",articleDao.getPage(user));/*分页数量*/
                        request.getSession().setAttribute("page",0);/*当前页面*/
                        request.getSession().setAttribute("cnt",0);/*数量控制*/
                        request.getSession().setAttribute("user", user);/*用户信息*/
                    } catch (SQLException sqlException) {
                        sqlException.printStackTrace();
                    }
                    //重定向转发页面，进入个人中心
                    response.sendRedirect(request.getContextPath() + "/personalHompage.jsp");
                }
            } else {
                /*登陆失败，重新登录*/
                System.out.println("登陆失败");
                response.sendRedirect(request.getContextPath() + "/loginPage.jsp");
            }
        }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
